package com.xh.bussiness.classinteractive.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.classinteractive.bean.QueryClassInteractiveCommonRequest;
import com.xh.bussiness.classinteractive.model.ClassInteractiveCommon;
import com.xh.bussiness.classinteractive.dao.ClassInteractiveCommonMapper;
import com.xh.bussiness.classinteractive.service.ClassInteractiveCommonService;
import com.xh.bussiness.system.model.Dictionary;
import com.xh.bussiness.system.service.IDictionaryService;
import com.xh.core.bean.XhPage;
import com.xh.core.service.impl.BaseServiceImpl;
import com.xh.core.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 课堂互动点评(TsClassInteractiveCommon)表服务实现类
 *
 * @author Pitt Cheung
 * @since 2021-07-09 14:12:32
 */
@Service
public class ClassInteractiveCommonServiceImpl extends BaseServiceImpl<ClassInteractiveCommonMapper, ClassInteractiveCommon> implements ClassInteractiveCommonService {

    @Autowired
    private IDictionaryService dictionaryService;

    @Override
    public ClassInteractiveCommon queryById(String id) {
        return this.getById(id);
    }

    @Override
    public XhPage<ClassInteractiveCommon> queryPage(XhPage page, QueryClassInteractiveCommonRequest request) {
        QueryWrapper qw = new QueryWrapper();
        if (StringUtils.isNotEmpty(request.getSubjectId())) {
            qw.eq("subject_id", request.getSubjectId());
        }
        if (StringUtils.isNotEmpty(request.getName())) {
            qw.like("common_name", request.getName());
        }
        if (request.getType() != null) {
            qw.eq("common_type", request.getType());
        }
        return (XhPage) this.dao.selectPage(page, qw);
    }

    @Override
    public boolean add(ClassInteractiveCommon classInteractiveCommon) {
        if (!CollectionUtils.isEmpty(classInteractiveCommon.getSubjectIds())) {
            for (String subject : classInteractiveCommon.getSubjectIds()) {
                Dictionary sub = dictionaryService.queryByCode(subject, "SUBJECT");
                ClassInteractiveCommon template = new ClassInteractiveCommon();
                BeanUtils.copyProperties(classInteractiveCommon, template);
                template.setSubjectId(subject);
                template.setSubjectName(sub.getName());

                this.insert(template);
            }
        }
        return true;
    }

    private List<ClassInteractiveCommon> queryBySubjectCode(String subjectCode,Integer type) {
        QueryWrapper<ClassInteractiveCommon> qw = new QueryWrapper<>();
        qw.eq("subject_id", subjectCode);
        if (type != null) {
            qw.eq("common_type", type);
        }
        return this.dao.selectList(qw);
    }


    @Override
    public boolean modify(ClassInteractiveCommon classInteractiveCommon) {
        return this.update(classInteractiveCommon);
    }

    @Override
    public boolean deleteByIds(String[] ids) {
        return this.batchDelete(ids);
    }

    @Override
    public void batchUpdateStatus(String[] ids, Integer status) {
        List<ClassInteractiveCommon> classInteractiveCommons = this.dao.selectBatchIds(Arrays.asList(ids));
        if (!CollectionUtils.isEmpty(classInteractiveCommons)) {
            for (ClassInteractiveCommon classInteractiveCommon : classInteractiveCommons) {
                classInteractiveCommon.setStatus(status);
                this.update(classInteractiveCommon);
            }
        }
    }
}
